# coding=utf-8
#================================================================
#   Copyright (C) 2020 * Ltd. All rights reserved.
#
#   File name   : rabbitmq.py
#   Author      : ZouZheKang-1996
#   Created date: 2020-08-14 18:33:08
#   Description : XuQingQing,I really ❤ U！
#
#================================================================

import pika

#简单模式发送
connection=pika.BlockingConnection(pika.ConnectionParameters(host='localhost'));#建立连接，localhost为自己的ip
channel=connection.channel() #开辟管道
channel.queue_declare("queue001")#声明队列，参数为队列名

#发送数据，发送一条，如果要发送多条则复制此段
channel.basic_publish(exchange = '',
                     routing_key = 'queue001',   # 队列名
                     body = 'hellow!',        #发送的数据
                     properties=pika.BasicProperties(delivery_mode=2))        # 让消息不丢失（内存→硬盘）

connection.close()

#简单模式接受
connection=pika.BlockingConnection(pika.ConnectionParameters(host='localhost'));#建立连接
channel=connection.channel()
def callback(channel, method, properties, body):
    print(body)
    channel.stop_consuming()
channel.basic_consume(queue = 'queue001',               #队列名
                      on_message_callback = callback,#回调函数
                      auto_ack = False)# no_ack = False，未收到确认标识，消息会重回队列。True，无论调用callback成功与否，消息都被消费掉
channel.start_consuming()